<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

require_once('login.php');

class USER_ADMIN extends CI_Controller {
	

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	  

	public function __construct()
	{
		parent::__construct();
		
		$this->load->helper('url');
		$this->load->library('session');
		$this->load->model('kit_cal_model');
		// $checkLogin = new login();
		$this->checkLoginWithCookie();
	}

	public function getNav(){
		include ('nav.php');
		$navClass = new nav();
		$nav = $navClass->nav();
		return $nav;
	}


	public function index()
	{


		$userData = $this->session->userdata('userData');

		if($userData){
			redirect('admin/login/loginWithCookie');
		}

		$this->load->view('admin/extra_login.html');
	}

	public function nav(){

			$nav = array(
			// '医学公式' => array( '新增公式'=>'#' ,'修改公式'=>'#'),
			// '随机化工具' => array( '查看使用情况' =>'#', )
					array(
							'name' => '医学公式',
							'icon' => 'fa-building-o',
							'children' => array(
								array('name' => '查看全部公式','url'=>$this->config->base_url().'admin/user_admin/checkCal',),
								array('name' => '新增公式','url' => $this->config->base_url().'admin/user_admin/add_cal_info', ),
								array('name' => '新增分类','url' => $this->config->base_url().'admin/user_admin/addCalCategory', ),
								array('name' => '回收站','url' => $this->config->base_url().'admin/user_admin/recycleCal', ),

							)
						),
					array(
							'name' => '随机化工具',
							'icon' => 'fa-suitcase',
							'children' => array(
								// array( 'name' => '查看','url' => '#')
							)
						),

				);
			return $nav;
		}


	public function checkLoginWithCookie(){

			$this->load->model('user_admin_model');	
			$userData = $this->session->userdata('userData');
			$username = '';
			$password = '';

			if($userData){

				$username = $userData['username'];
				$password = $userData['password'];

			}else{
				redirect($config['base_url'].'admin/login');
				return false;
			}

			$user_index = $this->user_admin_model->selectCode(array('login_name' => $username , 'password' => $password),'index');

			if($user_index){
				
				// $url = 'http://localhost/cal/admin/user_admin/highest_admin';
				// echo "<script>window.location.href='".$url."';</script>";
				 return true;

			}else{

				 redirect($config['base_url'].'admin/login');
				 return false;
			}
		}

	

	public function logOut(){
		$this->session->unset_userdata('userData');
		$this->checkLoginWithCookie();
	}

	public function highest_admin(){
		$userData = $this->session->userdata('userData');
		$username = $userData['username'];

		$nav = $this->nav();
		$this->data['nav'] = $nav;
		$this->data['username'] = $username;
		$this->load->view('admin/table_manage.html',$this->data);
	}

	public function add_cal_info(){
		// $nav = $this->nav();	
		// $this->data['nav'] = $nav;
		$title = '添加公式';
		$this->data['title'] = $title;
		$this->load->view('admin/table_add_cal.html',$this->data);
	}

	public function add_cal(){
		$cal_name = $this->input->post('cal_name');
		$cal_number = $this->input->post('cal_number');
		$cal_info = $this->input->post('cal_info');
		$cal_ari = $this->input->post('cal_ari');
		$english_name = $this->input->post('english_name');
		$status = 1;
		if(!is_numeric($cal_number)){
			echo "<script>alert('公式编号必须是数字!');history.go(-1)</script>";
			return false;
		}

		
		$isAllExi = $this->kit_cal_model->selectCode(array('cal_number' => $cal_number,'status'=>1),'cal_number');

		if($isAllExi){
			echo "<script>alert('已经存在相同编号的公式!');history.go(-1)</script>";
			return false;
		}

		$data = array(
				'cal_name'=>$cal_name,
				'cal_number' =>$cal_number,
				'cal_info' => $cal_info,
				'cal_ari' =>$cal_ari,
				'status' =>$status,
				'english_name' => $english_name
			);



		$insertId = $this->kit_cal_model->insert($data);

		if($insertId){

			echo "<script>alert('数据添加成功!')</script>";
			redirect('admin/user_admin/checkCal');

		}else{
			echo "<script>alert('数据添加失败！请重新输入。');history.go(-1)</script>";
		}
		
	}

	public function deleteCal(){//删除公式，未彻底删除
		$calId = $this->input->get('calId');
		$data = array(
				'status' => -1,
			);
		$where = array(
				'id' => $calId
			);		
		$data = $this->kit_cal_model->update($data,$where);

		$pageBeforeDeleteCal = array('url'=>$_SERVER['HTTP_REFERER']);//获取修改前的地址以便返回


		if($data){
			echo "<script>alert('公式删除成功！');</script>";
		}else{
			echo "<script>alert('公式删除失败！请重新尝试。');</script>";
		}

		redirect($pageBeforeDeleteCal['url']);

	}
	public function comDeleteCal(){//彻底删除公式
		$calId = $this->input->get('calId');
		$where = array(
				'id' => $calId
			);
		$data = $this->kit_cal_model->delete($where);
		if($data){
			echo "<script>alert('公式删除成功！');</script>";
		}else{
			echo "<script>alert('公式删除失败！请重新尝试。');</script>";
		}
		$pageBeforeDeleteCal = array('url'=>$_SERVER['HTTP_REFERER']);//获取修改前的地址以便返回
		redirect($pageBeforeDeleteCal['url']);
	}

	

	public function checkCal(){
		$currentPage = $this->input->get('page');
		$currentPage = empty($currentPage)? 1 :$currentPage;
		$this->load->model('kit_cal_model');

		$perPage = 10;//每页条数

		// $offset = $pagePer*($currentPage-1);
		// $currentPage = 1;//当前页数
		$where = array('status' =>1);
		$data = $this->kit_cal_model->get_page_data($where,$perPage,$currentPage,'cal_number');

		foreach ($data['data'] as $key => $value) {
				$data['data'][$key]['cal_info'] = mb_substr($value['cal_info'], 0,220,'utf-8');

			}	
				
		// $data['data']['cal_info'] = nl2br($data['data']['cal_info']);

		// foreach ($data['data'] as $key => $value) {
		// 	$value['cal_info'] = nl2br($value['cal_info']);
		// 	echo $value['cal_info'];
		// };
		$this->data['total'] = $data['total'];
		$this->data['all_cal'] = $data['data'];
		$this->data['pagePer'] = $perPage;
		$this->data['currentPage'] = $currentPage;
		$this->data['title'] = '全部公式';



		$this->load->view('admin/table_all_cal.html',$this->data);

	}

	public function modCalInfo(){

		// $this->load->library('parser');
		$calId = $this->input->get('calId');
		$this->load->model('kit_cal_model');

		$where = array(
				'id' => $calId
			);
		$what = 'cal_name,cal_info,cal_number,id,cal_ari,english_name';

		$data = $this->kit_cal_model->selectCode($where,$what);

		$this->data['data'] = $data;
		$this->data['title'] = '修改公式信息';

		// $this->parser->parse('admin/table_mod_cal.html', $data);
		$this->load->view('admin/table_mod_cal.html',$this->data);

		$pageBeforeModCal = array('url'=>$_SERVER['HTTP_REFERER']);//获取修改前的地址以便返回

		$this->session->set_userdata('pageBeforeModCal',$pageBeforeModCal);
	}

	public function modCal(){
		$calId = $this->input->get('calId');
		$cal_name = $this->input->post('cal_name');
		$cal_info = $this->input->post('cal_info');
		$cal_ari = $this->input->post('cal_ari');
		$english_name = $this->input->post('english_name');
		$this->load->model('kit_cal_model');


		$pageBeforeModCal = $this->session->userdata('pageBeforeModCal');
		$before = $pageBeforeModCal['url'];

		$data = array(
				'cal_name' => $cal_name,
				'cal_info' => $cal_info,
				'cal_ari' => $cal_ari,
				'english_name' =>$english_name
			);
		$where = array(
				'id' => $calId,
			);
		$query = $this->kit_cal_model->update($data,$where);

		if($query){

			redirect($before);	

		}else{
			echo "<script>alert('修改失败!');history.go(-1)</script>";
		}
	}

	public function searchCal(){
		$keyword = $this->input->post('keyword');
		$check = array(
				'status'=>1,
			);
		$data = $this->kit_cal_model->like('cal_name',$keyword,$check);

		
		$this->data['all_cal'] = $data;
		$this->data['title'] = '公式搜索';

		$this->load->view('admin/table_query_cal.html',$this->data);

	}

	public function recycleCal(){
		$currentPage = $this->input->get('page');
		$currentPage = empty($currentPage)? 1 :$currentPage;
		$this->load->model('kit_cal_model');

		$perPage = 10;//每页条数

		// $offset = $pagePer*($currentPage-1);
		// $currentPage = 1;//当前页数
		$where = array('status' =>-1);
		$data = $this->kit_cal_model->get_page_data($where,$perPage,$currentPage,'id');		
		
		// $data['data']['cal_info'] = nl2br($data['data']['cal_info']);

		// foreach ($data['data'] as $key => $value) {
		// 	$value['cal_info'] = nl2br($value['cal_info']);
		// 	echo $value['cal_info'];
		// };
		$this->data['total'] = $data['total'];
		$this->data['all_cal'] = $data['data'];
		$this->data['pagePer'] = $perPage;
		$this->data['currentPage'] = $currentPage;
		$this->data['title'] = '回收站';

		$this->load->view('admin/table_recycle_cal.html',$this->data);
	}

	public function doRecycleCal(){
		$calId = $this->input->get('calId');
		$cal_number = $this->input->get('cal_number');
		
		
		$where = array(
				'status' =>1,
				'cal_number' =>$cal_number
			);

		$pageBeforeDoRecycleCal = array('url'=>$_SERVER['HTTP_REFERER']);//获取修改前的地址以便返回
		

		$isExi = $this->kit_cal_model->selectCode($where,'id');

		if($isExi){
			echo "<script>alert('已存在相同编号的公式，请确认后再还原！');history.go(-1)</script>";
			
		}else{


			$query = $this->kit_cal_model->update(array('status'=>1),array('id'=>$calId));
			if($query){
				echo "<script>alert('还原成功！');</script>";
				redirect($pageBeforeDoRecycleCal['url']);
				
			}else{
				echo "<script>alert('还原失败！');history.go(-1)</script>";
				
			}

		}

		

	}

	public function addCalCategory(){

	}

	// public function mdUsed(){
	// 	$string = 'bioon8888';
	// 	$mdString = md5($string);
	// 	echo $mdString;
	// }


	private function userHeaderDirect($controller = '', $action = '', $params = array()){
    	if(!empty($params)){
	    	$getStr = "?" . http_build_query($params);
    	}
    	$url = config_item("base_url") . "{$controller}/{$action}{$getStr}";
		header("location: " . $url);
    }

    private function userDirect($controllerStr = 'user_admin', $actionStr = '', $params = array()){
    	
    	if(!empty($params)){
	    	$getStr = "?" . http_build_query($params);

    	}
        echo "<script>window.location.href='".config_item('base_url'). $controllerStr . "/" . $actionStr.$getStr."';</script>";
    }

     public static function chinesesubstr($str, $start, $len) { // $str指字符串,$start指字符串的起始位置，$len指字符串长度
     	$tmpstr ='';
        $strlen = $start + $len; // 用$strlen存储字符串的总长度，即从字符串的起始位置到字符串的总长度
        for($i = $start; $i < $strlen;) {
            if (ord ( substr ( $str, $i, 1 ) ) > 0xa0) { // 如果字符串中首个字节的ASCII序数值大于0xa0,则表示汉字
                $tmpstr .= substr ( $str, $i, 3 ); // 每次取出三位字符赋给变量$tmpstr，即等于一个汉字
                $i=$i+3; // 变量自加3
            } else{
                $tmpstr .= substr ( $str, $i, 1 ); // 如果不是汉字，则每次取出一位字符赋给变量$tmpstr
                $i++;
            }
        }
        return $tmpstr; // 返回字符串
    }
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */